home *** CD-ROM | disk | FTP | other *** search
/ Adobe Graphics & Publishing SDK 1996 December / Adobe Graphics & Publishing SDK 1996 December.iso / mac / PageMaker 6.5 SDK Mac / SourceCode / CW memory files / _GetmemWithFree.c next >
Encoding:
C/C++ Source or Header  |  1995-09-02  |  3.4 KB  |  115 lines  |  [TEXT/CWIE]

  1. /*     
  2.     This file or something like it should be used in code resources 
  3.     when malloc is used.  malloc allocates blocks of memory within which
  4.     it returns a pointer to the memory you request.  When you call free, the 
  5.     blocks aren't disposed.  This can be a problem in a code resource.  Call
  6.     FreeAllMallocMemory when you are done with the code resource and ready
  7.     to exit.  Since the runtime code has no way of knowing when you want 
  8.     or don't want the memory released, you are responsible to clean up 
  9.     after yourself.  This code does nothing with memory you explicitly 
  10.     allocate with NewPtr or NewHandle.  You need to keep track of that 
  11.     memory yourself and dispose of it when you are done.
  12.       
  13.     Please see the folder "Code Resource Examples:-CW Code Resource Info:
  14.     Memory Problems in CRs:FreeAllC++Memory:" on CW7 for an example of how 
  15.     to dispose of memory allocated with new.
  16.     
  17.     The malloc function CW uses doesn't use a data structure similar to 
  18.     mempools (used with new) that allows for easy freeing of the created blocks.  
  19.     I wrote a couple of functions and a replacement for the actual memory 
  20.     allocation function that keeps track of each block.  I also don't guarantee 
  21.     that this will work for all subsequent versions of CW.  Although I see no 
  22.     reason why they would change how malloc works, only use this file if you 
  23.     are willing to test that it is doing what you want. To test it, you would 
  24.     need to look in the heap to see if it is actually disposing the blocks. 
  25.     
  26.     This example has been tested with the ANSI 68K and PPC libraries. 
  27.     
  28.     Mark Anderson
  29.     metrowerks
  30.     12/16/94
  31. */
  32.  
  33. #include <stdlib.h>
  34. #include "FreeAllMallocMemory.h"
  35.  
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39.  
  40. void *    _Getmem(size_t size);
  41. void    PutInMallocList(Ptr  p);
  42.  
  43. #ifdef __cplusplus
  44. }
  45. #endif /* __cplusplus */
  46.  
  47. typedef struct    mallocLinkedList{
  48.     Ptr  list;
  49.     struct mallocLinkedList * next;
  50. }mallocLinkedList;
  51.  
  52. static mallocLinkedList * mallocList = NULL;
  53.  
  54. /*****************************************************************/
  55. /* Purpose..: Makes a mallocLinkedList, one for each block created */
  56. /*                by NewPtr in _GetMem                                   */
  57. /*****************************************************************/
  58. void    PutInMallocList(Ptr  p)
  59. {
  60.     mallocLinkedList * local = mallocList;
  61.     
  62.     if (local)
  63.     {
  64.         while (local->next)
  65.         {
  66.             local = local->next;
  67.         }
  68.         local->next = (mallocLinkedList*)NewPtr(sizeof(mallocLinkedList));
  69.         local = local->next;
  70.         local->next = NULL;
  71.         local->list = p;
  72.         
  73.     }
  74.     else    // initialize
  75.     {
  76.         mallocList = (mallocLinkedList*)NewPtr(sizeof(mallocLinkedList));
  77.         mallocList->next = NULL;
  78.         mallocList->list = p;
  79.     }
  80. }
  81.  
  82. /****************************************************************/
  83. /* Purpose..: Disposes of memory created by malloc and disposes */
  84. /*                of the linked list of those blocks.                */
  85. /****************************************************************/
  86. void FreeAllMallocMemory( void )
  87. {
  88.     mallocLinkedList * local = mallocList;
  89.     mallocLinkedList * temp;
  90.  
  91.     while (local)
  92.     {
  93.         temp = local;
  94.         DisposePtr(local->list);
  95.         local = local->next;
  96.         DisposePtr((Ptr)temp);        
  97.     }
  98.     mallocList = NULL;
  99.     
  100. }
  101.  
  102. /****************************************************************/
  103. /* Purpose..: Allocates memory for malloc.  Replaces version in */
  104. /*                ANSI library.                                     */
  105. /****************************************************************/
  106. void *_Getmem(size_t size)
  107. {
  108.     void *p;
  109.     size_t isize = size;
  110.     if (isize <= 0 || !(p = NewPtr(isize)))
  111.         return 0L;
  112.     PutInMallocList((Ptr)p);
  113.     return  p;
  114. }
  115.